package checkDetail;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONObject;
import weaver.conn.RecordSet;
import general.BaseBean;

public class WorkflowCustompageCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("WorkflowCustompageCheck checkData start");
        //定义查询sql
        String checkSql = "select id as workflowid, workflowname, custompage from workflow_base where custompage is not null";
        recordSet.executeSql(checkSql);
        List resultList = new ArrayList();
        String result = "";
        
        //获取项目路径
        //String rootPath = System.getProperty("user.dir");
        String rootPath = WorkflowCustompageCheck.class.getResource("").getPath();
        rootPath = rootPath.substring(0, rootPath.indexOf("/flowtoolbar/classes/checkDetail/"));
        
        while(recordSet.next()){
            //读取文件内容并按行检测
            readFileAndCheckFile(resultList, rootPath, recordSet);
        }
        if(resultList.size() == 0){
            result = resultOk;
            resultList.add("没有存在引入异常js的流程自定义页面。");
        }else{
            result = resultNeedFix;
        }
        (new BaseBean()).writeLog("WorkflowCustompageCheck checkData end");
        return formatReturnJsonCheck(result, resultList, this);
    }
    
    @Override
    public JSONObject fixData(HashMap dataMap) {
        return checkData(new HashMap());
    }

    @Override
    public String getClassName() {
        return "路径中customPage自定义页面检测";
    }
    
    @Override
    public String getClassDesc() {
        return "检测路径中customPage自定义页面是否有引用重复jquery.js和init.jsp问题";
    }
    
    /**
     * 检测页面是否包含jquery××××.js/init.jsp/init_wev8.jsp等
     * @param fileText customPage文件内容
     * @param jqueEx 正则
     * @return boolean boolean
     */
    private boolean checkFileTextEx(String fileText, String jqueEx) {
        Pattern pattern = Pattern.compile(jqueEx);
        Matcher matcher = pattern.matcher(fileText);
        // 查找字符串中是否有匹配正则表达式的字符串
        if(matcher.find())
        {
            return true;
        }
        return false;
    }
    
    /**
     * 读取文件内容并按行检测
     * @param resultList 
     * @param recordSet 
     * @param rootPath 
     */
    private void readFileAndCheckFile(List resultList, String rootPath, RecordSet recordSet) {
        //如果查询有值，循环检查自定义页面
        String workflowId = recordSet.getString("workflowid");
        String workflowName = recordSet.getString("workflowname");
        String customPage = recordSet.getString("custompage");
        File file = new File(rootPath + customPage);
        if(file.exists()){
          if(file.isFile()){
            try{
              BufferedReader input = new BufferedReader (new FileReader(file));
              String text;
              StringBuffer showMessage = new StringBuffer();
              while((text = input.readLine()) != null)
              {
                  //检测页面是否包含jquery××××.js
                  if(checkFileTextEx(text, "/jquery[-\\d\\.]*(min)?(_wev8)?\\.js"))
                  {
                      showMessage.append(formatMessage(workflowName, "路径中customPage：" + customPage + "页面引用了jquery.js，E8版本已经引用，无需重复引用，请手动修复！"));
                      showMessage.append("<br>");
                  }
                  
                  //检测页面是否包含init.jsp
                  if(checkFileTextEx(text, "/init.jsp"))
                  {
                      showMessage.append(formatMessage(workflowName, "路径中customPage：" + customPage + "页面引用了init.jsp，E8版本已经引用，无需重复引用，请手动修复！"));
                      showMessage.append("<br>");
                  }
                  
                  //检测页面是否包含/init_wev8.jsp
                  if(checkFileTextEx(text, "/init_wev8.jsp"))
                  {
                      showMessage.append(formatMessage(workflowName, "路径中customPage：" + customPage + "页面引用了init_wev8.jsp，E8版本已经引用，无需重复引用，请手动修复！"));
                      showMessage.append("<br>");
                  }
              }
              if(!"".equals(showMessage.toString())){
                  Map resultMap = new HashMap<String, String>();
                  resultMap.put("showMessage", showMessage.toString());
                  //resultMap.put("buttonname","修复");
                  resultMap.put("workflowid",workflowId);
                  resultList.add(resultMap);
              }
            }
            catch(IOException ioException){
               (new BaseBean()).writeLog("File Error!");
            }
          }
          else if(file.isDirectory()){
              (new BaseBean()).writeLog(resultList + "is Directory!");
          }
        }
        else{
            (new BaseBean()).writeLog(resultList + "Does not exist!");
        }
    }
}
